package net.jueb.custom.client;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import net.jueb.custom.protocol.NMsgDecoder;
import net.jueb.custom.protocol.NMsgEncoder;

public class Client {
	
	public void connect(String ip,int port)
	{
		NioEventLoopGroup group=new NioEventLoopGroup();
		Bootstrap bt=new Bootstrap();
		bt.group(group);
		bt.channel(NioSocketChannel.class);
		bt.handler(new ChannelInitializer<SocketChannel>() {

			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				//指定类型的解码器
				sc.pipeline().addLast(NMsgDecoder.class.getName(),new NMsgDecoder());
				sc.pipeline().addLast(NMsgEncoder.class.getName(),new NMsgEncoder());
				
				sc.pipeline().addLast(new ClientHandler());
			}
		});
		try {
			ChannelFuture cf=bt.connect(ip, port).sync();
			cf.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			group.shutdownGracefully();
		}
	}
	public static void main(String[] args) {
		new Client().connect("127.0.0.1", 80);
	}
}
